﻿using System;
using System.ComponentModel;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.Project.Import.Folder;
using Microsoft.WowAddonStudio.Project.Import;

namespace Microsoft.WowAddonStudio.Project.Import
{
	/// <summary>
	/// This page is used to pick the uri when folder export is the selected
	/// import type.
	/// </summary>
	public partial class CustomFolderPickerWizardPane : InnerPane
	{
		#region Lifecycle methods

		/// <summary>
		/// Creates instances of <see cref="CustomFolderPickerWizardPane"/>.
		/// </summary>
		public CustomFolderPickerWizardPane()
		{
			InitializeComponent();

			//Set resources
			var res = new ResourceManager(typeof (ImportWizardResources));
			HeaderText = res.GetString("ImportFolderPickerGroupBox");
			sourceLabel.Text = res.GetString("FolderImportLabel");
			sourceBrowseButton.Text = res.GetString("BrowseButton");

			SetActive += CustomFolderPickerWizardPane_SetActive;
		}

		/// <summary>
		/// Handles the SetActive event of the CustomFolderPickerWizardPane control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
		private void CustomFolderPickerWizardPane_SetActive(object sender, CancelEventArgs e)
		{
			HandleSettingsAndButtons();
		}

		/// <summary>
		/// Handles the settings and buttons.
		/// </summary>
		private void HandleSettingsAndButtons()
		{
			if (ValidSettings())
			{
				SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
				Settings = new FolderImportSettings(new Uri(sourceTextBox.Text));
			}
			else
			{
				SetWizardButtons(WizardButtons.Back);
				Settings = null;
			}
		}

		#endregion

		/// <summary>
		/// Handles the TextChanged event of the sourceTextBox control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void sourceTextBox_TextChanged(object sender, EventArgs e)
		{
			HandleSettingsAndButtons();
		}

		/// <summary>
		/// Handles the Click event of the sourceBrowseButton control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void sourceBrowseButton_Click(object sender, EventArgs e)
		{
			if (sourceFolderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				sourceTextBox.Text = sourceFolderBrowserDialog.SelectedPath;
			}
		}

		#region Private members

		/// <summary>
		/// Valids the settings.
		/// </summary>
		/// <returns></returns>
		private bool ValidSettings()
		{
			return Directory.Exists(sourceTextBox.Text);
		}

		#endregion
	}
}